home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Simple_Module2 / ParseWindow / ParseWindow.c < prev    next >
C/C++ Source or Header  |  1998-08-26  |  9KB  |  259 lines

  1. /*******************************************************************
  2.  
  3.    ParseWindow.c
  4.     
  5.     Does open a simple window with some easy gadgets and does
  6.     parse the supplied arguments.
  7.                     
  8. *********************************************************************/
  9. #define PARENT
  10. #include "/includes/Window.h"
  11.  
  12. // a template is already defined in our project.h (repeat ->)
  13. // "AutoClose/N,SmallBorder/K,Quit/S"
  14. // I want to do an additional work - we does parse the argument
  15. // supplied to "SmallBorder" again. So we need an additional template
  16.  
  17. #define ON_OFF_TEMPLATE "0/S,OFF/S,FALSE/S,NO/S,1/S,ON/S,TRUE/S,YES/S"
  18.  
  19. // maximum count of items in our second template 
  20. #define MAX_ON_OFF_COUNT 8
  21.  
  22. // if the result is higher or equal this, it means "on" - else "off" 
  23. #define ON_CASE          4
  24.  
  25. // we allow to use the first 4 to turn the small border off - but here
  26. // we don't do that, we show only the result of parsing to keep the
  27. // source easy.
  28.  
  29. /********************************************************************/
  30. // locale prototypes
  31.  
  32. BOOL OpenDOpusWin( WindowHandle *wh );
  33.  
  34. BOOL HandleWindow( WindowHandle *wh );
  35.  
  36. void ParseArguments( WindowHandle *wh, STRPTR args );
  37.  
  38. /********************************************************************/
  39.  
  40. void OwnWindow( STRPTR args, struct Screen *screen )
  41. {
  42.     WindowHandle          *wh;
  43.     
  44.     if( (wh = AllocMemH(mempool, sizeof(WindowHandle))) ) // allocate some memory
  45.       {
  46.           wh->screen = screen;     // store the screen pointer
  47.           
  48.           if( OpenDOpusWin(wh) )   // open the window
  49.            {
  50.                  // this we need to indicate that the second argument was empty
  51.                  wh->result = 9;
  52.                  
  53.                  // since we can not show the results we does not need to parse
  54.                  // the aguments earlier...
  55.                  
  56.                  ParseArguments( wh, args );
  57.                  
  58.                  // we copy now the arguments into the text gadget
  59.                // if we have supplied some...
  60.                  
  61.                SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) args );
  62.                                                    
  63.                while( TRUE ) 
  64.                   {                                         
  65.                       wh->signals = Wait( 1 << wh->win->UserPort->mp_SigBit );   // wait for window events
  66.                                                         
  67.                       if( wh->signals & 1 << wh->win->UserPort->mp_SigBit )
  68.                             if( HandleWindow(wh) )
  69.                                    break;        // does end the while loop                                       
  70.                      
  71.                   }
  72.                     
  73.                  if( wh->fargs )           // free the FuncArgs
  74.                        DisposeArgs( wh->fargs );
  75.              
  76.                CloseConfigWindow( wh->win ); 
  77.              }
  78.              
  79.           FreeMemH( wh ); // free our memory
  80.       }                            
  81. }
  82.  
  83. /********************************************************************/
  84. // This function does open our window. We could have done this in the
  85. // function OwnWindow() too, but we need this function later again.
  86.  
  87. BOOL OpenDOpusWin( WindowHandle *wh )
  88. {
  89.     NewConfigWindow ncfgwin;     // we need a NewConfigWindow structure too
  90.                                  // of couse you could also allocate it with
  91.                                           // AllocMemH()...
  92.     
  93.     // and have to fill it
  94.     
  95.     ncfgwin.nw_Parent = wh->screen;  // open on this screen
  96.     
  97.     // getting a localized title...
  98.     ncfgwin.nw_Title  = DOpusGetString( locale, MSG_WINDOW_TITLE );
  99.     
  100.     ncfgwin.nw_Dims   = &cfgwin; // a pointer to the ConfigWin structure
  101.     ncfgwin.nw_Locale = locale;  // the module locale pointer (from modinit.c)
  102.     ncfgwin.nw_Port   = NULL;    // we doesn't supply a port
  103.     ncfgwin.nw_Font   = NULL;    // just taking the screen font
  104.     ncfgwin.nw_Flags  = WINDOW_REQ_FILL      | // fill with stripple pattern
  105.                         WINDOW_AUTO_KEYS     | // handle keys automatic
  106.                               WINDOW_SCREEN_PARENT;  // nw_Parent points to a screen
  107.     
  108.     if( (wh->win = OpenConfigWindow(&ncfgwin)) )   // open the window
  109.       {
  110.           if( (wh->olist = AddObjectList(wh->win, odef)) ) // add the gadgets
  111.                   return TRUE;
  112.                     
  113.           CloseConfigWindow( wh->win );    //    in error case do not forget :-)
  114.       }
  115.     
  116.     return FALSE;
  117. }
  118.  
  119.  
  120. /********************************************************************/
  121. // we does only close the window, if the closegadget was pressed
  122. // if you want to close it within a gadget, you must only in the
  123. // right case set "stop" to TRUE
  124.  
  125. BOOL HandleWindow( WindowHandle *wh )
  126. {
  127.      BOOL stop = FALSE;
  128.      ULONG value;
  129.      
  130.      while( !stop && (wh->imsg = GetWindowMsg(wh->win->UserPort)) )
  131.         {
  132.             switch( wh->imsg->Class )  // let's handle the IDCMP
  133.               {
  134.                   case IDCMP_GADGETUP:
  135.                                           switch( GET_ID(wh->imsg) )
  136.                                          {
  137.                                                          case GADGET_ID_CYCLE:  // here we do show the results of our parsing or even a simple text
  138.                                                                                 
  139.                                                                                         if( wh->fargs )
  140.                                                                                           {
  141.                                                                                              value = GetGadgetValue( wh->olist, GADGET_ID_CYCLE );
  142.                                                                                              
  143.                                                                                               // value is now in range between 0 and 2 - just what we need... :) 
  144.                                                                                               
  145.                                                                                              switch( value )
  146.                                                                                                {
  147.                                                                                                      case 0:
  148.                                                                                                              sprintf( wh->buffer, "AutoClose value: %ld", wh->fargs->FA_Arguments[0] ? *(ULONG *) wh->fargs->FA_Arguments[0] : 0 );
  149.                                                                                                                 break;
  150.                                                                                                                 
  151.                                                                                                    case 1: 
  152.                                                                                                              if( wh->result == 9 )
  153.                                                                                                                     strcpy( wh->buffer, "No valid value for SmallBorder" );
  154.                                                                                                                 else
  155.                                                                                                                   {
  156.                                                                                                                       if( wh->result < ON_CASE )
  157.                                                                                                                            strcpy( wh->buffer, "SmallBorder goes off" );
  158.                                                                                                                       else
  159.                                                                                                                            strcpy( wh->buffer, "SmallBorder goes on" );
  160.                                                                                                                  }
  161.                                                                                                              break;
  162.                                                                                                                 
  163.                                                                                                    case 2: 
  164.                                                                                                              sprintf( wh->buffer, "Switch QUIT was %ssupplied", wh->fargs->FA_Arguments[2] ? NULL : "not " ); 
  165.                                                                                                              break;
  166.                                                                                                }
  167.                                                                                           }
  168.                                                                                         else
  169.                                                                                               strcpy( wh->buffer, "No arguments supplied" );
  170.                                                                                         
  171.                                                                             SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) wh->buffer );
  172.                                                                             break;
  173.             
  174.                                               case GADGET_ID_OKAY:   // doing a message
  175.                                                                      
  176.                                                                                         SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_OKAY_DONE) );
  177.                                                                             break;
  178.                                                      
  179.                                               case GADGET_ID_CANCEL: 
  180.                                                                      SetGadgetValue( wh->olist, GADGET_ID_TEXT, (ULONG) DOpusGetString(locale, MSG_CANCEL_DONE) );
  181.                                                                             break;
  182.                                           }
  183.                                                                  
  184.                                                   break;
  185.                                      
  186.                   case IDCMP_CLOSEWINDOW:
  187.                                           // we can not simply return here, the IntuiMessage must replied first
  188.                                                   
  189.                                                stop = TRUE;
  190.                                                   break;                                     
  191.               }
  192.                                  
  193.             ReplyWindowMsg( wh->imsg );  
  194.                               
  195.             // remember: You should not use any other routines
  196.             // to get/reply the messages of this window than
  197.             // GetWindowMsg() and ReplyWindowMsg() !!
  198.        }
  199.                                
  200.     return stop;
  201. }
  202.  
  203. void ParseArguments( WindowHandle *wh, STRPTR args )
  204. {
  205.       if( (wh->fargs = ParseArgs(FUNC1_TEMPLATE, args)) )
  206.         {
  207.              // now is already the main done
  208.              // if you want to access the result of parsing ->
  209.              
  210.              // for the first of the template (it's an ULONG value)
  211.              // result = *(ULONG *) wh->fargs->FA_Arguments[0];
  212.               
  213.              // for the second of the template (it's a STRPTR)
  214.              // result = wh->fargs->FA_Arguments[1];
  215.              
  216.              // for the last of the template (it's a BOOL)
  217.              // result = wh->fargs->FA_Arguments[2];
  218.              
  219.              // BUT REMEMBER :
  220.              // wh->fargs->FA_Arguments[x] itself is NULL, if the
  221.              // matching keyword was not supplied
  222.              // If you does not test this, an Enforcer hit is the
  223.              // "lowest" result you'll get... 
  224.              
  225.              // All clear ?
  226.              // Let's do it again -> we check now, if something
  227.              // and what of our ON_OFF_TEMPLATE was supplied to
  228.              // the SmallBorder argument 
  229.             
  230.              FuncArgs *fargs; // we need a second pointer
  231.                                        
  232.              if( !wh->fargs->FA_Arguments[1] )
  233.                   return;
  234.              
  235.              if( (fargs = ParseArgs(ON_OFF_TEMPLATE, (STRPTR) wh->fargs->FA_Arguments[1])) )
  236.                {
  237.                     // now we scan through the results and store them
  238.                     // as ULONG
  239.                                         
  240.                     for( wh->result = 0; wh->result < MAX_ON_OFF_COUNT; wh->result++ )
  241.                           if( fargs->FA_Arguments[wh->result] )
  242.                                  break;
  243.                     
  244.                     // we have now in wh->result:
  245.                     // - a value in range of 0 to 3 for any kind of "off"
  246.                     // - a value in range of 4 to 7 for any kind of "on"
  247.                     // - a value of 8, if nothing of the ON_OFF_TEMPLATE was supplied, but anything else...
  248.                     // - or even 9, if "SmallBorder" was not supplied or invalid
  249.                     
  250.                     DisposeArgs( fargs );
  251.                 }                    
  252.                
  253.              // you may also take a look to HandleWindow()
  254.              // I do there our output within the cycle and text gadget 
  255.              
  256.          }
  257.     
  258. }
  259.